<template>
	<view class="order-details">
		<view class="header-class">
			<view class="status_bar" style="height: var(--status-bar-height); width: 100%;"></view>			
			<tn-navbar height="45px" home-icon="" :bottom-shadow="false" bg-color="tn-type-primary_bg" text-color="tn-white">订单详情</tn-navbar>
		</view>

		<view class="order-info-card">
			<view class="info-item">
				<text class="label">客户名称：</text>
				<text class="content">{{orderInfoData.businessname}}</text>
			</view>	
			<view class="info-item">
				<text class="label">单据编号：</text>
				<text class="content">{{orderInfoData.billcode}}</text>
			</view>	
			<view class="info-item">
				<text class="label">下单人：</text>
				<text class="content">{{orderInfoData.caozyname}}</text>
			</view>	
			<view class="info-item">
				<text class="label">收货人：</text>
				<text class="content">{{orderInfoData.recemanname}} {{orderInfoData.recetelephone}}</text>
			</view>	
			<view class="info-item">
				<text class="label">收货地址：</text>
				<text class="content">{{orderInfoData.address}}</text>
			</view>	
			<view class="info-item" v-if="orderInfoData.remark">
				<text class="label">备注：</text>
				<text class="content">{{orderInfoData.remark}}</text>
			</view>	
			<view class="info-item" v-show="orderInfoData.isht!= undefined">
				<tn-switch v-model="orderInfoData.isht" :active-value="1" :inactive-value="0" active-text="打印合同" inactive-text="不打印合同" />
			</view>
		</view>

		<view class="goods-list">
			<view class="goods-item" v-for="(item, index) in orderInfoData.details" :key="item.goodsname">
				<view class="goods-name">
					{{item.goodsname}}
				</view>
				<view class="goods-spec">
					<text class="spec">{{item.goodsspec}}</text>
					<text class="unit">{{item.wmsmeas}}</text>
				</view>	
				<view class="goods-manufacturer">
					{{item.manufacturer}}
				</view>
				<view class="goods-batch">
					{{item.batchinfo}}
				</view>
				<view class="goods-price-info">
					<text class="quantity">{{item.num}}</text>
					<text class="price">{{item.taxprice}}</text>
				</view>	
				<view class="goods-amount">
					<text class="highlight-text">{{item.taxamount}}</text>
				</view>
				<view class="goods-inner-info" v-if="item.innerpriceinfo">
					<text class="inner-price">{{item.innerpriceinfo}}</text>
					<text class="inner-amount">{{item.inneramountinfo}}</text>
				</view>
				<view class="input-section" v-if="userInfo.islocal=='Y'">
					<view class="input-row">
						<text class="input-label">结算价:</text>
						<tn-input v-model="item.innerprice" placeholder="请输入结算价" type="number" class="price-input"/>
					</view>
					<view class="input-row">
						<text class="input-label">备注:</text>
						<tn-input v-model="remark" placeholder="请输入备注" class="remark-input"/>
					</view>
					<view class="modify-button">
						<tn-button type="success" @click="modifyShopping(item,index)">修改</tn-button>
					</view>
				</view>
			</view>
		</view>

		<view class="bottom-buttons">
			<view class="total-amount">
				<text class="amount-label">合计金额</text>
				<text class="amount-value">¥{{orderInfoData.taxamount}}</text>
			</view>
			<view class="action-buttons">
				<tn-button type="danger" size="lg" @click="toUnEnd">驳回</tn-button>
				<tn-button type="success" size="lg" @click="toEnd">通过</tn-button>
			</view>
		</view>
	</view>
</template>

<script setup>
	import { ref,reactive,onMounted } from 'vue'
	import { postCall,postReturnPay } from '@/api/index.js'
	import { EventBus } from '@/mitt/EventBus.js';
	import { toastMessage } from '@/utils/toast.js'
	import { createParams } from '@/utils/utils.js'
	import { v4 as uuidv4 } from 'uuid';
	
	let userInfo = reactive({});
	const procName = "PROC_APP_DO_ORDER";
	const payConfig = {};
	let orderInfoData = reactive({});
	let remark = ref("");
	onMounted(()=>{
		const tempOrderInfo = uni.getStorageSync("ORDERINFO");
		Object.assign(orderInfoData,tempOrderInfo);
		doGetPayConfig();
		userInfo = uni.getStorageSync('USERINFO');
	})
	 
	let doGetPayConfig = async () =>{
		let params = {};
		params["P_PROC_NAME"] = "PROC_APP_DO_SHOPPING";
		params["P_ACTION"] = "PAYCONFIG";		
		await postCall(params).then(res=>{
			if(res){
				if(res.code===200){
					Object.assign(payConfig,res.data.DATASET[0]);
				}
			}
			
		})
	}
	 

	let toUnEnd = async ()=>{
		if(orderInfoData.tradeid){
			console.log("000000000");
			let oldtrxid = orderInfoData.tradeid;
			let trxamt = parseFloat(orderInfoData.taxamount);
			await onLineReturn(oldtrxid,trxamt);
		}
		
		let params = {};
		params["P_PROC_NAME"] = "PROC_APP_DO_ORDER";
		params["P_ACTION"] = "NO";
		params["P_BILLNO"] = orderInfoData.billno;
		await postCall(params).then(res=>{
			if(res){
				if(res.code===200){
					toastMessage("驳回成功！");
					uni.navigateTo({
					  url: '/pages/order/unendorder'
					})
				}
			}
		})  
		// onLineReturn(oldtrxid,trxamt).then(res=>{
		// 	let params = {};
		// 	params["P_PROC_NAME"] = "PROC_APP_DO_ORDER";
		// 	params["P_ACTION"] = "NO";
		// 	params["P_BILLNO"] = orderInfoData.billno;
		// 	await postCall(params).then(res=>{
		// 		if(res){
		// 			if(res.code===200){
		// 			    toastMessage("驳回成功！");
		// 			    uni.navigateTo({
		// 				  url: '/pages/order/unendorder'
		// 			    })
		// 			}
		// 		}
			   
		// 	})  
			
		// })
	}
	
	let toEnd =()=>{
		let params = {};
		params["P_PROC_NAME"] = "PROC_APP_DO_ORDER";
		params["P_ACTION"] = "YES";
		params["P_BILLNO"] = orderInfoData.billno;
		// console.log(Object.assign(createParams(orderInfoData),params));
		postCall(Object.assign(createParams(orderInfoData),params) ).then(res=>{
			if(res){
				if(res.code===200){
							   toastMessage("添加成功！");
							   uni.navigateTo({
							   	url: '/pages/order/unendorder'
							   })
				}
			}
		}) 
	}
	
	let onLineReturn = async (oldtrxid,trxamt) =>{
		let guid = uuidv4();
		let params = {};
		params.cusid = payConfig.cusid;
		params.appid = payConfig.appid;
		params.version = "11";
		params.reqsn = guid;
		params.trxamt = (trxamt*100).toString();
		params.oldtrxid = oldtrxid;
		params.randomstr = guid;
		params.signtype = payConfig.signtype;
		await postReturnPay(params).then(res=>{
			if(res){
				if(res.code===200){
					if(res.data.errmsg){
						toastMessage(res.data.errmsg)
					}
					if(res.data.trxstatus){
						if(res.data.trxstatus=="0000"){
							toastMessage("作废成功！");
						}
					}
				}
			}
		})
	}
	
	const modifyShopping=(val,index)=>{
		let params = {};
		params["P_PROC_NAME"] = "PROC_APP_DO_ORDER";
		params["P_ACTION"] = "LOCALMODIFY";
		params["P_INNERPRICE"] = val.innerprice;
		params["P_BILLCODESN"] = val.billcodesn;
		params["P_BILLCODE"] = orderInfoData.billcode;
		params["P_REMARK"] = remark.value;
		postCall(params).then(res=>{
			if(res){
				if(res.code===200){
					toastMessage("修改成功！");
				}
			}
		})
		
	}
	
</script>

<style>
.order-details {
	background-color: #f5f7fa;
	min-height: 100vh;
	padding-bottom: 120rpx;
}

.header-class {
	background: var(--tn-color-primary);
	padding-bottom: 15rpx;
}

.order-info-card {
	background: #fff;
	margin: 20rpx;
	padding: 30rpx;
	border-radius: 12rpx;
	box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.04);
}

.info-item {
	padding: 16rpx 0;
	font-size: 28rpx;
	border-bottom: 2rpx solid #f5f5f5;
}

.info-item:last-child {
	border-bottom: none;
}

.label {
	color: #666;
	margin-right: 10rpx;
}

.content {
	color: #333;
}

.goods-list {
	margin: 20rpx;
}

.goods-item {
	background: #fff;
	padding: 30rpx;
	margin-bottom: 20rpx;
	border-radius: 12rpx;
	box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.04);
}

.goods-name {
	font-size: 32rpx;
	font-weight: 500;
	color: #333;
	margin-bottom: 20rpx;
}

.goods-spec {
	display: flex;
	justify-content: space-between;
	font-size: 28rpx;
	color: #666;
	margin-bottom: 16rpx;
}

.goods-manufacturer {
	font-size: 28rpx;
	color: #666;
	margin-bottom: 16rpx;
}

.goods-batch {
	font-size: 28rpx;
	color: #666;
	margin-bottom: 16rpx;
}

.goods-price-info {
	display: flex;
	justify-content: space-between;
	font-size: 28rpx;
	color: #666;
	margin-bottom: 16rpx;
}

.goods-amount {
	font-size: 28rpx;
	color: #666;
	margin-bottom: 16rpx;
}

.highlight-text {
	color: #ff6b6b;
	font-weight: 500;
}

.goods-inner-info {
	display: flex;
	justify-content: space-between;
	font-size: 28rpx;
	color: #666;
	margin-bottom: 16rpx;
}

.input-section {
	margin-top: 20rpx;
	padding-top: 20rpx;
	border-top: 2rpx solid #f5f5f5;
}

.input-row {
	display: flex;
	align-items: center;
	margin-bottom: 16rpx;
}

.input-label {
	width: 120rpx;
	font-size: 28rpx;
	color: #333;
	font-weight: bold;
}

.price-input, .remark-input {
	flex: 1;
}

.modify-button {
	display: flex;
	justify-content: flex-end;
	margin-top: 16rpx;
}

.bottom-buttons {
	position: fixed;
	bottom: 0;
	left: 0;
	width: 100%;
	background-color: #fff;
	padding: 20rpx 30rpx;
	display: flex;
	justify-content: space-between;
	align-items: center;
	box-shadow: 0 -2rpx 12rpx rgba(0, 0, 0, 0.08);
	padding-bottom: calc(env(safe-area-inset-bottom) + 20rpx);
}

.total-amount {
	flex: 1;
	display: flex;
	flex-direction: column;
	justify-content: center;
}

.amount-label {
	font-size: 24rpx;
	color: #666;
	margin-bottom: 4rpx;
}

.amount-value {
	font-size: 36rpx;
	color: #ff6b6b;
	font-weight: bold;
}

.action-buttons {
	flex: 2;
	display: flex;
	gap: 20rpx;
	padding-left: 30rpx;
}

.action-buttons :deep(.tn-button) {
	flex: 1;
	height: 80rpx;
	border-radius: 40rpx;
	font-size: 30rpx;
	font-weight: 500;
}
</style>
